

import { createRouter, createWebHashHistory } from 'vue-router';


const modules = import.meta.glob('../pages/**/*.vue')
export function filterAsyncRoutes(routes) {
  const res = []
  routes.forEach(route => {
      let tmp = { ...route }
      if (tmp.children) {
          tmp.children = filterAsyncRoutes(tmp.children)
      }
      if (Object.prototype.toString.call(tmp.component) === '[object String]') {
        tmp.component = modules[`../pages/${route.component}`];
      }
      res.push(tmp)
  })
  return res
}


// 公共路由
export const constantRoutes = [
	{
	  path: '/',
	  component: 'Demo/index.vue',
	},
  {
    path: '/demo',
    component: 'Demo/index.vue',
  },
  {
    path: '/404',
    component: '404/index.vue',
  },
  {
    path: '/login',
    component: 'Login/index.vue',
  },
]

const routers = filterAsyncRoutes(constantRoutes);

const router = createRouter({
  history: createWebHashHistory(), // history 模式则使用 createWebHistory()
  routes: routers
});


export default router;